package com.ninong.ker.admin.rest;

import com.ninong.ker.admin.domain.FwFormConfig;
import com.ninong.ker.admin.service.FwFormConfigService;
import com.ninong.ker.admin.service.dto.FwFormConfigDTO;
import com.ninong.ker.common.tools.enums.BusinessEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ninong.ker.common.annotation.perm.CmdAdmin;
import com.ninong.ker.common.response.CmdResponse;


/**
 * 表单配置
 *
 * @author 鸠摩智
 * @email dongmingchao@pian-ai.com
 * @date 2021-02-07 17:00:45
 */
@RestController
@RequestMapping("/cmd/v1/formconfig")
public class FwFormConfigResouces {

    @Autowired
    private FwFormConfigService fwFormConfigService;

    /**
     * 列表
     */
    @CmdAdmin({"admin", "form:list"})
    @PostMapping
    public ResponseEntity<CmdResponse> list(@RequestBody FwFormConfigDTO dto) {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.listPage(dto));
        return ResponseEntity.ok(response);
    }


    @CmdAdmin({"admin", "form:delete"})
    @DeleteMapping
    public ResponseEntity<CmdResponse> delete(@RequestBody String[] ids) {
        fwFormConfigService.delete(ids);
        CmdResponse response = new CmdResponse();
        response.setMessage(BusinessEnum.DELETE_SUCCESS.getMsg());
        return ResponseEntity.ok(response);
    }

    @CmdAdmin({"admin", "form:add", "form:update"})
    @PutMapping
    public ResponseEntity<CmdResponse> create(@RequestBody FwFormConfig entity) throws Exception {
        fwFormConfigService.create(entity);
        return ResponseEntity.ok(new CmdResponse());
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<CmdResponse> get(@PathVariable String id) {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.get(id));
        return ResponseEntity.ok(response);
    }

    /**
     * 表单集合
     * @return
     */
    @GetMapping("/treeForm")
    public ResponseEntity<CmdResponse> get() {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.treeForm());
        return ResponseEntity.ok(response);
    }

}
